package com.sjhang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sjhang.entity.TbArticle;
import com.sjhang.mapper.TbArticleMapper;
import com.sjhang.service.TbArticleService;
import com.sjhang.utils.ListParamVo;
import com.sjhang.utils.ResultResponse;
import com.sjhang.vo.ArticleVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 技术文章表  服务实现类
 */
@Service
public class TbArticleServiceImpl extends ServiceImpl<TbArticleMapper, TbArticle> implements TbArticleService {

    @Resource
    private TbArticleMapper tbArticleMapper;

    /***
     * @description 查询所有技术文章
     */
    @Override
    public ResultResponse selectArticle(ListParamVo listParamVo) {
        IPage<ArticleVo> list = tbArticleMapper.selectbyArticle(
                new Page<TbArticle>(listParamVo.getCurrent(), listParamVo.getSize()),
                listParamVo.getParamMap().get("title"), listParamVo.getParamMap().get("type"),
                listParamVo.getParamMap().get("teachid"), listParamVo.getParamMap().get("userName")
        );
        return ResultResponse.SUCCESS(list);
    }

    /***
     * @description 删除自己的技术文章
     */
    @Override
    public ResultResponse deleteArticle(Long listParamVo) {
        //判断id是否为空
        if(listParamVo != null){
            tbArticleMapper.deleteById(listParamVo);
            return ResultResponse.SUCCESS();
        }
        return ResultResponse.FAILED();
    }

    /***
     * @description 发布文章
     */
    @Override
    public ResultResponse insertArticle(TbArticle listParamVo) {
        if(listParamVo != null){
            tbArticleMapper.insert(listParamVo);
            return ResultResponse.SUCCESS();
        }
        return ResultResponse.FAILED();
    }


    /***
     * @description 修改文章
     * @params [listParamVo]
     */
    @Override
    public ResultResponse updateArticle(TbArticle listParamVo) {
        if(listParamVo != null){
            TbArticle articleID = tbArticleMapper.selectOne(
                    new QueryWrapper<TbArticle>().lambda().eq(TbArticle::getArticleId, listParamVo.getArticleId()).last("limit 1")
            );

            articleID.setArticleTitle(listParamVo.getArticleTitle());
            articleID.setArticleType(listParamVo.getArticleType());
            articleID.setArticleShow(listParamVo.getArticleShow());

            tbArticleMapper.updateById(articleID);

            return ResultResponse.SUCCESS();
        }
        return ResultResponse.FAILED();
    }
}


